package com.bootcamp.javase2.network;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @program: day22NetworkCode
 * @description: after 3 seconds write time per 5 seconds.
 * @author: Keyu Li
 * @create: 2021-09-16 19:38
 **/

public class StopWatchTask {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new WriteTimeTask(), 3000,5000);
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行完毕！");
        timer.cancel();
    }
}

class WriteTimeTask extends TimerTask{

    @Override
    public void run() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            FileWriter writer = new FileWriter("current time list.txt",true);
            writer.write(dateFormat.format(new Date()));
            writer.write(System.lineSeparator());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}